﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using NHibernate.Type;
using NHibernate.SqlTypes;
using System.Data.Common;
using System.IO;
using System.Xml;
using System.Web.DomainServices;

namespace FlexiCommerce.Components
{
    [Serializable]
    public class Configuration : PersistenceItem
    {
        private Store store = new Store();
        public Store Store
        {
            get { return store; }
            set { store = value; }
        }
        [Exclude]
        public string StoreXml
        {
            get { return this.Serialize<Store>(store); }
            set { this.store = this.Deserialize<Store>(value); }
        }
        public int ThumbnailHeight
        {
            get { return store.ThumbnailHeight; }
            set { store.ThumbnailHeight = value; }
        }

        public int ThumbnailWidth
        {
            get { return store.ThumbnailWidth; }
            set { store.ThumbnailWidth = value; }
        }

        public int TinyHeight
        {
            get { return store.TinyHeight; }
            set { store.TinyHeight = value; }
        }

        public int TinyWidth
        {
            get { return store.TinyWidth; }
            set { store.TinyWidth = value; }
        }


        public string Serialize<T>(T value)
        {
            String XmlizedString = null;

            MemoryStream memoryStream = new MemoryStream();

            XmlSerializer xs = new XmlSerializer(typeof(T));

            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            xs.Serialize(xmlTextWriter, value);

            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());

            return XmlizedString;
        }
        private String UTF8ByteArrayToString(Byte[] characters)
        {

            UTF8Encoding encoding = new UTF8Encoding();

            String constructedString = encoding.GetString(characters);

            return (constructedString);

        }
        private Byte[] StringToUTF8ByteArray(String pXmlString)
        {

            UTF8Encoding encoding = new UTF8Encoding();

            Byte[] byteArray = encoding.GetBytes(pXmlString);

            return byteArray;

        }
        public T Deserialize<T>(string value)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));

            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(value));

            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);



            return (T)xs.Deserialize(memoryStream);
        }

    }


    [XmlRootAttribute("Store")]
    public class Store
    {

        private int thumbnailHeight;
        [XmlAttribute("thumbnailHeight")]
        public int ThumbnailHeight
        {
            get { return thumbnailHeight; }
            set { thumbnailHeight = value; }
        }

        private int thumbnailWidth;
        [XmlAttribute("thumbnailWidth")]
        public int ThumbnailWidth
        {
            get { return thumbnailWidth; }
            set { thumbnailWidth = value; }
        }

        private int tinyHeight;
        [XmlAttribute("tinyHeight")]
        public int TinyHeight
        {
            get { return tinyHeight; }
            set { tinyHeight = value; }
        }

        private int tinyWidth;
        [XmlAttribute("tinyWidth")]
        public int TinyWidth
        {
            get { return tinyWidth; }
            set { tinyWidth = value; }
        }


    }
}
//    public abstract class SeriazableField<T> : PrimitiveType
//        where T: class
//    {

//        public SeriazableField()
//            : base(SqlTypeFactory.GetString(2147483647))
//        {
//        }

//        public abstract string Serialize(T value);
//        public abstract T Deserialize(string value);
//        public override object DefaultValue
//        {
//            get { return ""; }
//        }

//        public override string ObjectToSQLString(object value, NHibernate.Dialect.Dialect dialect)
//        {
//            return this.Serialize(value as T);
//        }

//        public override Type PrimitiveClass
//        {
//            get { return typeof(T); }
//        }

//        public override object FromStringValue(string xml)
//        {
//            return this.Deserialize(xml);
//        }

//        public override object Get(System.Data.IDataReader rs, string name)
//        {
//            return FromStringValue(rs[name].ToString());
//        }

//        public override object Get(System.Data.IDataReader rs, int index)
//        {
//            return FromStringValue(rs[index].ToString());
//        }

//        public override void Set(System.Data.IDbCommand cmd, object value, int index)
//        {
//            DbParameter param = cmd.Parameters[index] as DbParameter;
//            param.Value = this.Serialize(value as T);
//        }

//        public override string Name
//        {
//            get { return typeof(T).Name; }
//        }

//        public override Type ReturnedClass
//        {
//            get { return typeof(T); }
//        }
       
//    }
//    public abstract class XmlSeriazableField<T> : SeriazableField<T>
//        where T: class
//    {

//        public override string Serialize(T value)
//        {
//            String XmlizedString = null;

//            MemoryStream memoryStream = new MemoryStream();

//            XmlSerializer xs = new XmlSerializer(typeof(Store));

//            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

//            xs.Serialize(xmlTextWriter, value);

//            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

//            XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());

//            return XmlizedString;
//        }
//        private String UTF8ByteArrayToString(Byte[] characters)
//        {

//            UTF8Encoding encoding = new UTF8Encoding();

//            String constructedString = encoding.GetString(characters);

//            return (constructedString);

//        }
//        private Byte[] StringToUTF8ByteArray(String pXmlString)
//        {

//            UTF8Encoding encoding = new UTF8Encoding();

//            Byte[] byteArray = encoding.GetBytes(pXmlString);

//            return byteArray;

//        } 
//        public override T Deserialize(string value)
//        {
//            XmlSerializer xs = new XmlSerializer(typeof(T));

//            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(value));

//            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);



//            return (T)xs.Deserialize(memoryStream);
//        }
//    }
//}
